# encoding: utf-8

from PyQt5.Qt import *
import math


class MyWindow(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__()
        # self.resize(1200, 800)
        # 无标题栏无边框的窗口
        self.setWindowFlags(Qt.FramelessWindowHint)
        # 窗口大小不可改变
        self.setFixedSize(kwargs['width'] if 'width' in kwargs else 300, kwargs['height'] if 'height' in kwargs else 250)
        # 半透明
        self.setWindowOpacity(0.95)
        # 设置id
        self.setObjectName('home')
        self.setProperty('my_window_type', 'normal')
        # 初始化点击flag
        self.press_flag = False
        # 设置窗口图标
        icon = QIcon(r'widget\img\erfg.png')
        self.setWindowIcon(icon)
        self.setAttribute(Qt.WA_StyledBackground, True)   # 修改widget背景的必须操作

        self.init_normal_btn()
        self.setup_ui()

    def setup_ui(self):
        pass

    def paintEvent(self, event):
        painter = QPainter(self)
        # pixmap = QPixmap("myPic.png")
        # painter.drawPixmap(self.rect(), pixmap)

        pen1 = QPen(Qt.white, 3)
        painter.setPen(pen1)
        painter.drawLine(1, 1, self.rect().width() - 1, 1)
        painter.drawLine(1, 1, 1, self.rect().height() - 1)
        pen2 = QPen(Qt.lightGray, 2)
        painter.setPen(pen2)
        painter.drawLine(self.rect().width() - 1, 1, self.rect().width() - 1, self.rect().height() - 1)
        painter.drawLine(1, self.rect().height() - 1, self.rect().width() - 1, self.rect().height() - 1)

    # 设置关闭、最小化按钮 start -------------------------------------------
    # 按钮初始化
    def init_normal_btn(self):
        # 关闭按钮
        self.close_btn = QPushButton(self)
        self.close_btn.setProperty('my_btn_type', 'close_btn')
        self.close_btn.clicked.connect(self.close)

        # 最小化按钮
        self.min_btn = QPushButton(self)
        self.min_btn.setProperty('my_btn_type', 'min_btn')
        self.min_btn.clicked.connect(self.showMinimized)

    # 监听窗口大小变化，动态设置标题栏按钮的位置
    def resizeEvent(self, a0: QResizeEvent) -> None:
        self.close_btn.move(int(self.width()) - 30, 0)
        self.min_btn.move(int(self.width()) - 60, 0)
    # 设置关闭、最小化按钮 end -------------------------------------------

    # 窗口拖拽功能 start ----------------------------------------------------
    # 监听鼠标按下
    def mousePressEvent(self, a0: QMouseEvent) -> None:
        self.mouse_x = a0.globalX()
        self.mouse_y = a0.globalY()
        self.window_x = self.x()
        self.window_y = self.y()
        # print(math.fabs(self.window_y - self.mouse_y))
        if math.fabs(self.window_y - self.mouse_y) < 50:
            self.press_flag = True
        else:
            self.press_flag = False

    # 监听鼠标拖拽
    def mouseMoveEvent(self, a0: QMouseEvent) -> None:
        if self.press_flag:
            move_x = a0.globalX() - self.mouse_x
            move_y = a0.globalY() - self.mouse_y
            new_window_x = self.window_x + move_x
            new_window_y = self.window_y + move_y
            self.move(new_window_x, new_window_y)

    # 监听鼠标释放
    def mouseReleaseEvent(self, a0: QMouseEvent) -> None:
        self.press_flag = False
    # 窗口拖拽功能 end ----------------------------------------------------


class ExtendedComboBox(QComboBox):
    def __init__(self, parent=None):
        super(ExtendedComboBox, self).__init__(parent)

        self.setFocusPolicy(Qt.StrongFocus)
        self.setEditable(True)

        # add a filter model to filter matching items
        self.pFilterModel = QSortFilterProxyModel(self)
        self.pFilterModel.setFilterCaseSensitivity(Qt.CaseInsensitive)
        self.pFilterModel.setSourceModel(self.model())

        # add a completer, which uses the filter model
        self.completer = QCompleter(self.pFilterModel, self)
        # always show all (filtered) completions
        self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
        self.setCompleter(self.completer)

        # connect signals
        self.lineEdit().textEdited.connect(self.pFilterModel.setFilterFixedString)
        self.completer.activated.connect(self.on_completer_activated)

    # on selection of an item from the completer, select the corresponding item from combobox
    def on_completer_activated(self, text):
        if text:
            index = self.findText(text)
            self.setCurrentIndex(index)
            self.activated[str].emit(self.itemText(index))

    # on model change, update the models of the filter and completer as well
    def setModel(self, model):
        super(ExtendedComboBox, self).setModel(model)
        self.pFilterModel.setSourceModel(model)
        self.completer.setModel(self.pFilterModel)

    # on model column change, update the model column of the filter and completer as well
    def setModelColumn(self, column):
        self.completer.setCompletionColumn(column)
        self.pFilterModel.setFilterKeyColumn(column)
        super(ExtendedComboBox, self).setModelColumn(column)